<!DOCTYPE html>
<html class="ui-page-login">

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="format-detection" content="telephone=no,email=no" />
		<title>金豆充值</title>
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link href="../css/style.css" rel="stylesheet" />
		<style>
			.jd-info{
				padding:15px;
				/*background-color: #FFF;*/
			}
			.jd-info .sum{
				float: right;
				color: #dd524d;
				font-size: 20px;
			}
			.jd-info table{
				float: left
			}
			.mui-btn {
				padding: 10px;
				word-spacing:25px;
			}
		</style>

	</head>
	
	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">金豆充值</h1>
		</header>
		<div id="jd-pay" class="mui-content">
			<div class="jd-info">
				<label>账户金豆余额:</label>
				<div class="sum">0000</div>
			</div>
			<div class="pay-form">
				<form id='card-form' class="mui-input-group">
					<div class="mui-input-row">
						<label>金额：</label>
						<input id='money' type="text" class="mui-input-clear mui-input" placeholder="请输入充值金额" maxlength="12">
					</div>
					
				</form>
			</div>
			
			
			<div class="mui-content-padded">
				<button id='cardinfo' class="mui-btn mui-btn-block mui-btn-danger">充         值</button>
			</div>
			
		</div>
		<script src="../js/mui.min.js"></script>
		<script src="../js/app.js"></script>
		<script>
		(function($, doc) {
		   $.init({
					swipeBack:true,
					beforeback:function(){
					mui.fire(plus.webview.getWebviewById('user/usercenter.html'),'updateData');
					}
				});	
	       	$.plusReady(function() {
	       		
	       		plus.payment.getChannels(function(channels){
					var content=document.getElementById('dcontent');
					for(var i in channels){
						var channel=channels[i];
						if(channel.id=='alipay' /*|| channel.id=='wxpay'*/){
							pays[channel.id]=channel;
							var de=document.createElement('div');
							de.setAttribute('class','custom-btn');
							de.setAttribute('onclick','pay(this.id)');
							de.id=channel.id;
							de.innerText=channel.description+"支付";
							content.appendChild(de);
							checkServices(channel);
						}
					}
				},function(e){
					plus.nativeUI.alert("获取支付方式失败："+e.message);
				});
	       		
	       		var toMain = function() {
						$.fire(plus.webview.getWebviewById('user/usercenter.html'),'updateData');
					};
					var cardButton = doc.getElementById('cardinfo');
					var money = doc.getElementById('money');
					var moneyrge=/^\d+(\.\d{1,2})$/;
					cardButton.addEventListener('tap', function(event) {
						
						if(money.value.length != 0){
							if(!moneyrge.test(money.value)){
								return	plus.nativeUI.toast("请填写正确的金额");
							}
						}else{
							return plus.nativeUI.toast("金额不能为空");
						}
						
						var cardInfo = {
							money: money.value
						};
						
						
						plus.nativeUI.alert("ssss")
						 toMain();
					
					});
					
						// 检测是否安装支付服务
						function checkServices(pc){
							if(!pc.serviceReady){
								var txt=null;
								switch(pc.id){
									case "alipay":
									txt="检测到系统未安装“支付宝快捷支付服务”，无法完成支付操作，是否立即安装？";
									break;
									default:
									txt="系统未安装“"+pc.description+"”服务，无法完成支付，是否立即安装？";
									break;
								}
								plus.nativeUI.confirm(txt,function(e){
									if(e.index==0){
										pc.installService();
									}
								},pc.description);
							}
						}
						
						var wPay=null;
						// 支付
						function pay(id){
							if(wPay){return;}//检查是否请求订单中
							if(id=='alipay'||id=='wxpay'){
								//url+=id;
							}else{
								plus.nativeUI.alert("不支持此支付通道！");
								return;
							}
							
							wPay=plus.nativeUI.showWaiting();
							// 请求支付订单
							var xhr=new XMLHttpRequest();
							xhr.onreadystatechange=function(){
								switch(xhr.readyState){
									case 4:
									wPay.close();wPay=null;
									if(xhr.status==200){
										var order=JSON.parse(xhr.responseText);
										//没进这里
										
										plus.payment.request(pays[id],order[0].url,
										function(result){
											plus.nativeUI.alert("支付成功，等待卖家发货！",function(){
												openOrderList(3)
											});
										},
										function(e){
											plus.nativeUI.alert("订单支付失败！"+e.code,function(){
												
												openOrderList(1)
											});
										});
										
									}else{
										plus.nativeUI.alert("获取订单信息失败！");
									}
									break;
									default:
									break;
								}
							}
							//支付接口
							xhr.open('GET',URL+'api/payment/GetPaymentList?orderIds='+payOrderId);
							xhr.send();
						}
				
	       	});
		}(mui, document));
		</script>
	</body>

</html>
